package com.project.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.*;

import com.project.dao.implement.AccountDataStatisticalImpl;

public class AccountDataStatisticalServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public AccountDataStatisticalServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the GET method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html/json;charset=utf-8");
		request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        
        String userName =request.getParameter("username");
        String userBankAccount =request.getParameter("userBankAccount");
        String userCreateActBank =request.getParameter("userCreateActBank");
        
        Map<String,Object> param = new HashMap<String,Object>();
    	param.put("userAccountNum", userBankAccount);
    	param.put("username", userName);
    	param.put("userCreateActBank", userCreateActBank);
    	
    	AccountDataStatisticalImpl accountDataMapper = new AccountDataStatisticalImpl();
    	
    	long inFundCount = accountDataMapper.SelectInFundCount(param);
    	double inFundMoney = accountDataMapper.SelectInFundMoney(param);
    	long outFundCount = accountDataMapper.SelectOutFundCount(param);
    	double outFundMoney = accountDataMapper.SelectOutFundMoney(param);
    	List<String> inAccountList = accountDataMapper.SelectInAccountNumCount(param);
    	List<String> outAccountList = accountDataMapper.SelectOutAccountNumCount(param);
    	
    	Set set=new HashSet();
    	Iterator inIt = inAccountList.iterator();
    	while(inIt.hasNext()){ 
    		set.add(inIt.next());
    	}
    	
    	Iterator outIt = inAccountList.iterator();
    	while(outIt.hasNext()){ 
    		set.add(outIt.next());
    	}
    	
    	int accountCount = set.size();
    	
    	JSONObject json = new JSONObject(); 
    	try{                          
    		json.put("inFundCount", inFundCount);   
    		json.put("inFundMoney", inFundMoney);  
    		json.put("outFundCount", outFundCount);  
    		json.put("outFundMoney", outFundMoney);    
    		json.put("accountNumCount", accountCount);   
    		json.put("accountCount", accountCount);   
    	}
    	catch(Exception e){               
    		e.printStackTrace();           
    	}           
    	System.out.println(json.toString());           
    	response.getWriter().write(json.toString());  
    	
    	
        
		/*
		PrintWriter out = response.getWriter();
		out.print("    <span>This is a");
		out.print(this.getClass());
		out.print("inFundCount");
		out.print(inFundCount);
		out.print(", inFundMoney");
		out.print(inFundMoney);
		out.print(", outFundCount");
		out.print(outFundCount);
		out.print(", outFundMoney");
		out.print(outFundMoney);
		out.println(", using the POST method</span>");
		out.flush();
		out.close();*/
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
